<?php
	$Meses = meses();

	$primero_de_mes        = mktime(0, 0, 0, date("m")  , 1, date("Y"));
	$mes_anterior  = mktime(0, 0, 0, date("m")-1, 1,   date("Y"));

	
	//------------------------------------------------------------------------------------
	//   Tengo Reservas para este alojamiento?
	//------------------------------------------------------------------------------------
	$sql = "select * from reservas where producto_id='{$Producto['id']}' order by desde ASC";
	$rs  = $db->Execute($sql);
	$ReservasOriginal = $rs->GetRows();
	$Reservas = array();
	$Ocupado = array();
	if($ReservasOriginal) {
		foreach($ReservasOriginal as $ro){
		
			$desde = $ro['desde'];
			$cantidad = $ro['cantidad_noches'];			
			if ($ro['tipo']==1){
				for($i=0;$i<=$cantidad;$i++){	
					$fecha = mktime(0,0,0,date("m",$desde),date("d",$desde)+$i,date("Y",$desde));
					$Reservas[$fecha] = $ro['nombre'];
				}
			} else {
				for($i=0;$i<=$cantidad;$i++){	
					$fecha = mktime(0,0,0,date("m",$desde),date("d",$desde)+$i,date("Y",$desde));
					$Ocupado[$fecha] = $ro['nombre'];
				}
			}
		}
	
	
	}
	
	
	
	$Rango = array();
	for($i=1;$i<=4;$i++){
		$new  = mktime(0, 0, 0, date("m",$mes_anterior)+$i, 1,   date("Y",$mes_anterior));
		
		$inicio = mktime(0, 0, 0, date("m",$new), 1,   date("Y",$new));
		$proximo  = mktime(0, 0, 0, date("m",$new)+1, 1,   date("Y",$new));
		
		$fin_de_mes  = mktime(0, 0, 0, date("m",$proximo), date("d",$proximo)-1,   date("Y",$proximo));
		
		$dias_del_mes = date("d",$fin_de_mes);
		
		$fila = 1;
		
		for($d=1;$d<=$dias_del_mes;$d++){
				
			$dia = mktime(0, 0, 0, date("m",$fin_de_mes), $d,   date("Y",$fin_de_mes));
			
			$w = iif( date("w",$dia)==0,7,date("w",$dia) ) * 1;

		
			$Rango[$new][$fila][$w] = $dia;
			
			$fila = iif($w==7,$fila+1,$fila);
		
		}
		
		
	}
	
	$hoy = mktime(0,0,0,date("m"),date("d"),date("Y"));

?>	
	<table style='font-size:11px;margin-bottom:10px;margin-left:30px;line-height:12px;'>
		<tr>
			<td class='hoy referencia'>&nbsp;</td><td style='padding-right:40px;'>&nbsp;Hoy</td>
			<td class='feriado referencia'>&nbsp;</td><td style='padding-right:40px;'>&nbsp;Feriado</td>
			<td class='ocupado referencia'>&nbsp;</td><td  style='padding-right:40px;'>&nbsp;Ocupado</td>
			<td class='reserva referencia'>&nbsp;</td><td>&nbsp;Reservado</td>
		</tr>
	</table>
	<div class='clearfix'></div>
	<div class='row-fluid'>
<?php
	
	$x =1;
	foreach($Rango as $clave=>$r){
		echo "<div class='span3' style='min-width:200px;'>";

			
		$mes = $Meses[date("m",$clave)*1]."/".date("Y",$clave);
		echo "<table class='tbl-calendar'>
			<tr ><thead><td colspan='7'>$mes</td></thead></tr>
			<tr class='titulos-dias'>
				<td>LUN</td>
				<td>MAR</td>
				<td>MIE</td>
				<td>JUE</td>
				<td>VIE</td>
				<td>SAB</td>
				<td>DOM</td>
			</tr>";
				
		foreach($r as $fila=>$valor){
			echo "<tr>";			
			foreach($valor as $col=>$data){
						
				if($fila==1 and !isset($ya_paso_por_aca)){
					if($col==2){ echo "<td class='vacia'>&nbsp;</td>";}	
					if($col==3){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==4){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==5){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==6){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					if($col==7){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
					$ya_paso_por_aca = true;
				}
						
				$dia_de_hoy = iif($data==$hoy,'hoy','');
				$es_feriado = ''; //iif($data==$feriado,'feriado','');
						
				$reserva    = iif(isset($Reservas[$data]),'reserva','');
				$ocupado    = iif(isset($Ocupado[$data]),'ocupado','');
						
				$title='';
				if(!empty($reserva)){	$title = $Reservas[$data]; }
				if(!empty($ocupado)){	$title = $Ocupado[$data]; }
						
							

				echo "<td id='id-$data' class='$ocupado $dia_de_hoy $es_feriado $reserva'>".date("d",$data)."</td>";	
			}
			if($col==6){ echo "<td class='vacia'>&nbsp;</td>";}	
			if($col==5){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
			if($col==4){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
			if($col==3){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
			if($col==2){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	
			if($col==1){ echo "<td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td><td class='vacia'>&nbsp;</td>";}	

			echo "</tr>"; // Termina una fila de la semana
				
		}
				
		unset($ya_paso_por_aca);
				
		echo "</table>";
		echo "</div>";	
	}
?>
	</div>
<style>
.hoy{
	border:2px solid #333 !important;
}

.tbl-calendar {
	border:1px solid #FEBA02;
	width:200px;
	font-size:12px;
	line-height:12px;
}
thead{
	background:#FEBA02;
	text-align:center;
	color:#333;
	font-weight:bold;
	line-height:20px;
}
.titulos-dias{
	background:#F7E3AD;
	text-align:center;
	color:#333;
	font-weight:bold;
	line-height:20px;
}
.vacia {
	background:#F7F2E5;
}
.tbl-calendar td {
	border:1px solid #FEBA02;
	text-align:center;
	font-size:11px;
}

.td-contenedora{
	vertical-align:top;
	padding:0px 5px;

}

.referencia {
	width:20px;
}
.ocupado{
	background:#FF0000;
}
.reserva{
	background:#13A552;
}

.feriado{
	border:2px solid #1D3FB6 !important;
}

</style>